#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2019-03-20 15:44:49

import os
import time
import signal
import threading
from multiprocessing import Process

import psutil


def worker():
    # 忽略信号
    signal.signal(signal.SIGTERM, lambda a, b: None)

    # 新线程执行的代码:
    def loop():
        print('thread %s is running...' % threading.current_thread().name)
        for n in range(100):
            print('thread %s >>> %s' % (threading.current_thread().name, n))
            time.sleep(1)
        print('thread %s ended.' % threading.current_thread().name)

    print('thread %s is running...' % threading.current_thread().name)
    t = threading.Thread(target=loop, name='LoopThread', daemon=True)
    t.start()
    proc = psutil.Process()
    pproc = proc.parent()
    print(t.is_alive(), pproc.is_running())
    while t.is_alive() and pproc.is_running():
        print("wait...")
        time.sleep(10)
    print('thread %s ended.' % threading.current_thread().name)


print('Parent process %s.' % os.getpid())
p = Process(target=worker)
print('Child process will start.')
p.start()
p.join()
print('Parent process end.')
